package hot;

import java.util.Arrays;

public class Num581 {
    public int findUnsortedSubarray(int[] nums) {
        int[] arr = Arrays.copyOf(nums,nums.length);
        Arrays.sort(arr);
        int l = 0,r = nums.length - 1;
        while(l < nums.length) {
            if(nums[l] == arr[l]) l++;
            else break;
        }
        while(r >= 0) {
            if(nums[r] == arr[r]) r--;
            else break;
        }
        return l < r? (r - l + 1) : 0;
    }
}
